package com.laker.autotest.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.laker.autotest.AutoTestApplication;
import com.laker.autotest.config.EasyConfig;
import com.laker.autotest.core.BasePageTest;
import com.laker.autotest.core.LakerEmailableReporter2;
import com.laker.autotest.core.ScreenshotListener;
import com.laker.autotest.core.SendEmailListener;
import com.laker.autotest.core.retry.RetryAnnotationTransformer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;
import org.testng.ITestResult;
import org.testng.TestNG;
import org.testng.reporters.EmailableReporter2;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
public class TestNGUtil {

    public static final String TESTNG_XML = "testng.xml";

    public static String getTestNgXmlPath() {
        ApplicationHome ah = new ApplicationHome(AutoTestApplication.class);
        File jarFile = ah.getSource();
        InputStream testngStream = AutoTestApplication.class.getClassLoader().getResourceAsStream(TESTNG_XML);
        File xmlFile = new File(jarFile.getParent() + "/" + TESTNG_XML);
        if (!xmlFile.exists()) {
            FileUtil.writeFromStream(testngStream, xmlFile);
        }
        return xmlFile.getPath();
    }

    public static void runWithTestNgXmlPath(String testNgXmlPath) {
        try {
            EasyConfig easyConfig = SpringUtil.getBean(EasyConfig.class);
            TestNG testNG = new TestNG(false);
            List<String> config = new ArrayList<>();
            config.add(testNgXmlPath);
            // send email
            if (easyConfig.isSendEmail()) {
                testNG.addListener(new SendEmailListener());
            }
            // report
            testNG.setOutputDirectory(TestNGUtil.getReportDir());
            if (easyConfig.isEnhancedReport()) {
                testNG.addListener(new LakerEmailableReporter2());
            } else {
                EmailableReporter2 reporter2 = new EmailableReporter2();
                reporter2.setFileName(TestNGUtil.getReportFileName());
                testNG.addListener(reporter2);
            }
            // screenshot
            if (easyConfig.getTraceOptions().screenshotEnable) {
                testNG.addListener(new ScreenshotListener());
            }
            // retry
            testNG.addListener(new RetryAnnotationTransformer());
            testNG.setTestSuites(config);
            testNG.setUseDefaultListeners(false);
            testNG.run();
        } catch (Exception e) {
            log.error("runWithTestNgXmlPath error", e);
        }
    }

    public static String getScreenShotPath(ITestResult tr) {
        BasePageTest basePage = (BasePageTest) tr.getInstance();
        String trName = tr.getName();
        return "screenshot/" + basePage.hashCode() + "-" + basePage.getClass().getSimpleName() + "-" + trName + ".png";
    }

    public static String getTracePath(ITestResult tr) {
        BasePageTest basePage = (BasePageTest) tr.getInstance();
        String trName = tr.getName();
        return "trace/" + DateUtil.format(new Date(), "yyyy-MM-dd_HH-mm-ss") + "_" + basePage.hashCode()
                + "_" + basePage.getClass().getSimpleName() + "-" + trName + ".zip";
    }


    public static String getReportDir() {
        return "report";
    }

    public static String getReportFileName() {
        return "email-report.html";
    }
}
